package com.lou;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author xgluo
 * @program: makePackage
 * @name: CopyFiles
 * @description: 将指定路径下的文件和编译后的文件输出到，path文件所在目录
 * @date 2025年02月27日
 */
public class CopyFilesPHP {

    // 获取源文件的所在项目的基础路径
    private String projectDir;
    private String patchDir ;
//    private String classDir;
//    private String webContextDir;

    public CopyFilesPHP() {
        ReadConfigFile instance = ReadConfigFile.getInstance();
        if(instance == null){
            RuntimeException runtimeException = new RuntimeException("未配制项目路径，请先完善配制文件");
            runtimeException.printStackTrace();
            return;
        }
        projectDir = instance.getProjectPath();
        patchDir = instance.getPhpPatchPath();
//        classDir = instance.getClassPath();
//        webContextDir = instance.getWebcontextPath();
    }

    /**
     * 根据路径创建文件到指定目录，目录不存在则创建
     * @param path svn修改的文件路径
     */
    public void copyFiles(String path) {
        String patchPath = getPatchPath();
        Set<String> uniquePaths = new HashSet<>(); // 用于存储去重后的路径

        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) continue;

                // 去重逻辑：如果路径已存在则跳过
                if (uniquePaths.add(line)) { // 添加成功（不重复）则处理
                    String toPath = patchPath + line;
                    String fromPath = projectDir + line;
                    Path pathDest = Paths.get(toPath);
                    Path parentDir = pathDest.getParent();

                    if (parentDir != null && !Files.exists(parentDir)) {
                        Files.createDirectories(parentDir);
                    }

                    System.out.println("文件:" + fromPath + "  ==>  " + toPath);
                    Files.copy(Paths.get(fromPath), pathDest, StandardCopyOption.REPLACE_EXISTING);
                }
            }
        } catch (IOException e) {
            System.err.println("错误: " + e.getMessage() + " 路径: " + path);
            e.printStackTrace();
        }
    }

    /**
     * @description: 根据源文件路径生成目标文件路径
     * @return destFilePath
     * @date 2025/3/4
     * @author xgluo
     */
    private String getPatchPath() {

        return patchDir+"/"+"pctch_"+ DateUtil.format(LocalDateTime.now(),DateUtil.DATETIME_FORMAT_)  +"/";

    }

    /**
     * @description: java文件需要额外生成class文件名
     * @param filePath
     * @return destFilePath
     * @date 2025/3/4
     * @author xgluo
     */
//    public File[] getClassFileList(String filePath) {
//        // 获取父目录
//        int i = 0;
//        if(filePath.startsWith(PathConstant.JAVA_PREFFIX)){
//            i = filePath.indexOf("/", 1);
//            filePath = projectDir+classDir+"/"+filePath.substring(i+1, filePath.length()).replaceAll(".java",".class");
//        }else{
//            System.out.println(filePath);
//        }
//        Path pathSour = Paths.get(filePath);
//        String fileName = pathSour.getFileName().toString();
//        Path pathSourParent = pathSour.getParent();
//        File file = new File(pathSourParent.toString());
//        File[] files = file.listFiles(new FilenameFilter() {
//            @Override
//            public boolean accept(File pathname, String name) {
//                if(fileName.equals(name)){
//                    return true;
//                }else{
//                    return name.contains(fileName.substring(0,fileName.indexOf(".")) + "$");
//                }
//            }
//        });
//
//        return files;
//    }

}
